/*
 * Поиск в тексте
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LNUM_MAX	('z' - 'a' + 1)

struct tnode {
	struct tnode *next[LNUM_MAX];
	char *prefix;
	long len;
	int final;
};

void
add(struct tnode *trie, const char *s)
{
	struct tnode *node = trie;
	int i = 0, j = 0;
	register int index;

	while (s[i]) {
		node = node->next[s[i] - 'a'];
		if (node == NULL) {
			node->next[s[i] - 'a'] = (struct tnode *)
				calloc(1UL, sizeof (struct tnode));
			node->next[s[i] - 'a']->prefix = strdup(s[i]);
			node = node->next[index];
			node->final = 1;
		} else {
			while (s[i] == node->prefix[j]) {
			}
		}
	}
}

int
contains(struct tnode *trie, const char *s)
{
	struct tnode *node = trie;
	register int i = 0;
	for (; s[i]; ++i) 
		if (node->next[s[i] - 'a'])
			node = node->next[s[i] - 'a'];
		else
			return 0;
	return 1;
}

void
free_trie(struct tnode *lroot)
{
	register short i = 0;

	while (i < LNUM_MAX)
		if (lroot->next[i])
			free_trie(lroot->next[i]);

	if (lroot->prefix)
		free(lroot->prefix);
	free(lroot);
}

/*
int
contains(struct tnode *node, cosnt char *s)
{
	struct tnode *next = node->next[*s - 'a'];
	register int i;

	if (!*s)
		return 1;
	if (next) {
		for (i = 1; s[i] && i < next->len; ++i)
			if (s[i] != next->s[i])
				return 0;
		if (!s[i])
			return 1;
		else
			return contains(next, s + i);
	}
}
*/

int
main(void)
{
	static char text[5002];
	int i = 0, n;
	static char str[5002];
	struct tnode *root;

	root = (struct tnode *) calloc(1UL, sizeof *root);

	(void) fgets(text, 5001, stdin);
	while (i < strlen(text))
		add(root, &text[i++]);

	scanf("%d", &n);
	fprintf(stderr, "\t\tn=%d\n", n);
	for (i = 0; i < n; ++i) {
		fgets(str, 5001, stdin);
		if (contains(root, str))
			fputs(str, stdout);
	}

	free_trie(root);
	return 0;
}
